package org.wattdepot.cli.commands;

import java.util.List;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.jaxb.SubSources;
import org.wattdepot.resource.source.summary.jaxb.SourceSummary;

/**
 * Provides the summary of a given source.
 * 
 * @author David Joel Lazaro
 */
public class SummaryListCommand implements Command {

  /**
   * Calls the listSummary method.
   * 
   * @param userInput The user input.
   * @param client The WattDepotClient
   * @return Returns the method call with the proper parameters.
   */
  public String doCommand(List<String> userInput, WattDepotClient client) {
    return listSummary(userInput, client);
  }

  /**
   * Outputs the summary of information for a given source.
   * 
   * @param commandParam The input from user.
   * @param client The WattDepotClient
   * @return Returns the summary of the source.
   */
  private String listSummary(List<String> commandParam, WattDepotClient client) {
    StringBuffer buff = new StringBuffer(200);
    SourceChecker check = new SourceChecker();
    List<String> sourcesList = null;
    if (!check.sourceChecker(commandParam.get(1), client)) {
      return "Invalid Source";
    }
    try {
      Source theSource = client.getSource(commandParam.get(1));
      buff.append("Subsources: ");

      if (theSource.getSubSources() == null) {
        buff.append("none");
      }
      else {
        SubSources subSources = theSource.getSubSources();
        sourcesList = subSources.getHref();

        for (String list : sourcesList) {
          String[] split = list.split("/");
          buff.append(split[5] + " ");
        }
      }

      buff.append("\nDescription: ");
      buff.append(theSource.getDescription());

      String split[] = theSource.getOwner().split("/");
      String owner = split[5];
      buff.append("\nOwner: ");
      buff.append(owner);

      buff.append("\nLocation: ");
      buff.append(theSource.getLocation());
      buff.append("\nCoordinates: ");
      buff.append(theSource.getCoordinates());
      buff.append("\nProperties: ");
      buff.append(theSource.getProperties());

      SourceSummary summary = client.getSourceSummary(commandParam.get(1));
      buff.append("\nEarliest data: ");
      buff.append(summary.getFirstSensorData());
      buff.append("\nLatest data: ");
      buff.append(summary.getLastSensorData());
      buff.append("\nTotal data points: ");
      buff.append(summary.getTotalSensorDatas());

    }
    catch (NotAuthorizedException e) {
      e.printStackTrace();
    }
    catch (ResourceNotFoundException e) {
      e.printStackTrace();
    }
    catch (BadXmlException e) {
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      e.printStackTrace();
    }

    return buff.toString();
  }

}
